package com.water.wfo.user.controller;

import com.water.wfo.component.api.entity.response.BasicResponse;
import com.water.wfo.component.util.MD5Util;
import com.water.wfo.sys.pojo.entity.RoleEntity;
import com.water.wfo.sys.pojo.entity.UserEntity;
import com.water.wfo.sys.pojo.request.RoleRequest;
import com.water.wfo.sys.pojo.response.RoleResponse;
import com.water.wfo.sys.pojo.response.UserResponse;
import com.water.wfo.user.ability.RoleAbility;
import com.water.wfo.user.api.RoleApi;
import com.water.wfo.user.business.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * packageName com.water.wfo.user.controller
 *
 * @author
 * @className RoleController(处以class为网)
 * @date 2024/8/30
 * @description T0D0
 */
@Slf4j
@RestController
public class RoleController implements RoleApi {
    @Autowired
    private RoleAbility roleAbility;
    @Autowired
    private IRoleService roleService;

    @Override
    public BasicResponse<RoleResponse> queryRoleListInfo(@RequestBody RoleRequest request) {
        BasicResponse<RoleResponse> basicResponse = new BasicResponse<>();
        if(null == request){
            return basicResponse.buildFail("入参为空");
        }
        try {
            RoleResponse response = roleService.queryRoleListInfo(request);
            return basicResponse.buildSuccess(response);
        } catch (Exception e) {
            log.error("查询角色集合异常:",e);
            return  basicResponse.buildFail("查询客户集合异常");
        }
    }

    @Override
    public BasicResponse<String> addRole(@RequestBody RoleEntity roleEntity) {
        BasicResponse<String> basicResponse = new BasicResponse<>();
        if(null == roleEntity){
            return basicResponse.buildFail("入参为空");
        }
        try {
            roleEntity.setActive("Y");
            roleAbility.insert(roleEntity);
            return basicResponse.buildSuccess();
        } catch (Exception e) {
            log.error("新增角色异常:",e);
            return  basicResponse.buildFail(e.getMessage());
        }
    }

    @Override
    public BasicResponse<String> editeRole(@RequestBody RoleEntity roleEntity) {
        BasicResponse<String> basicResponse = new BasicResponse<>();
        if(null == roleEntity){
            return basicResponse.buildFail("入参为空");
        }
        try {
            roleAbility.update(roleEntity);
            return basicResponse.buildSuccess();
        } catch (Exception e) {
            log.error("修改用户异常:",e);
            return  basicResponse.buildFail(e.getMessage());
        }
    }

    @Override
    public BasicResponse<String> deleteRole(@RequestParam("id") Long roleId) {
        BasicResponse<String> basicResponse = new BasicResponse<>();
        if(null == roleId){
            return basicResponse.buildFail("入参为空");
        }
        try {
            RoleEntity roleEntity = new RoleEntity();
            roleEntity.setId(roleId);
            roleEntity.setActive("N");
            roleAbility.update(roleEntity);
            return basicResponse.buildSuccess();
        } catch (Exception e) {
            log.error("重置密码异常:",e);
            return  basicResponse.buildFail(e.getMessage());
        }
    }
}
